<?php
namespace ImiApp\Module\Member\Service;

use Imi\Bean\Annotation\Bean;
use Imi\Aop\Annotation\Inject;
use Imi\ConnectContext;
use Imi\RequestContext;
use Imi\Server\Session\Session;

// use Imi\Server\Server;
// use Imi\Controller\WebSocketController;
// use Imi\Server\Route\Annotation\WebSocket\WSRoute;
// use Imi\Server\Route\Annotation\WebSocket\WSAction;
// use Imi\Server\Route\Annotation\WebSocket\WSController;


use \Imi\Redis\Redis;


/**
 * @Bean("MemberSessionService")
 */
class MemberSessionService 
{
    /**
     * @Inject("ScUserService")
     *
     * @var \ImiApp\Module\Member\Service\ScUserService
     */
    protected $memberService;

    /**
     * 用户ID
     *
     * @var int
     */
    protected $memberId;

    /**
     * 用户信息
     *
     * @var \ImiApp\Module\Member\Model\ScUser
     */
    protected $memberInfo;

    /**
     * 是否登录
     *
     * @var boolean
     */
    protected $isLogin = false;

    public function __init()
    {
        $this->init();
    }

    /**
     * 初始化
     *
     * @return void
     */
    public function init()
    {
 


        /** @var \Imi\Server\Http\Message\Request $request */
        $request = \Imi\RequestContext::get('request');

        // var_dump('httpRouteResult', ConnectContext::get('routeResult'));

        // var_dump('request->getHeaders()',$request);

        // $sessionId = $request->getHeaderLine('X-Session-Id');
        
        // if(!$sessionId){
        if($request){

        
            $sessionId = $request->get('_token');
            var_dump('_token:',$sessionId);
        }
        // }      

        if($fd = RequestContext::get('fd'))
        {
            // var_dump('MemberSessionService->fd',$fd);
            $memberId = ConnectContext::get('memberId', null, $fd);//注释
            // var_dump('MemberSessionService->memberId',$memberId);
            if(!$memberId)
            $memberId = Redis::get('tp:'.hash_hmac('ripemd160', $sessionId, 'i3d6o32wo8fvs1fvdpwens'));
            // var_dump('user_id',$memberId);
        
        }
        else
        {
            $memberId = false;
        }
        // var_dump('fd',$fd);
        
        // var_dump('memberId',$memberId);

        if(!$memberId)
        {
            $memberId = Session::get('memberId');
        }
        
        if(!$memberId)
        {
            return;
        }
        $this->memberId = $memberId;
        $this->isLogin = true;



    }

    /**
     * 是否登录
     *
     * @return boolean
     */
    public function isLogin()
    {
        // var_dump($this->getMemberInfo()['username']);
        return $this->isLogin;
    }

    /**
     * Get 用户信息
     *
     * @return \ImiApp\Module\Member\Model\ScUser
     */ 
    public function getMemberInfo()
    {
        if(!$this->memberInfo)
        {
            var_dump('$this->memberId',$this->memberId);
            $this->memberInfo = $this->memberService->get($this->memberId);
        }
        return $this->memberInfo;
    }

    /**
     * Get 用户ID
     *
     * @return int
     */ 
    public function getMemberId()
    {
        return $this->memberId;
    }

}
